package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.base.IBaseEnum;
import com.settlement.system.common.enums.SysOrderStatusEnum;
import com.settlement.system.common.util.NumberUtils;
import com.settlement.system.common.util.StringUtils;
import com.settlement.system.mapper.SysOrderMapper;
import com.settlement.system.model.entity.SysAddress;
import com.settlement.system.model.entity.SysOrder;
import com.settlement.system.model.form.SysOrderForm;
import com.settlement.system.model.query.SysOrderQuery;
import com.settlement.system.model.vo.SysOrderImportVo;
import com.settlement.system.service.SysAddressService;
import com.settlement.system.service.SysOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 刷单表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-07-17
 */
@Service
@RequiredArgsConstructor
public class SysOrderServiceImpl extends ServiceImpl<SysOrderMapper, SysOrder> implements SysOrderService {

    private final SysAddressService addressService;
    @Override
    public Page<SysOrder> getPage(SysOrderQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysOrder> page = new Page<>(pageNum, pageSize);
        Page<SysOrder> mapperPage = this.baseMapper.getPage(page, queryParams);
        if (mapperPage.getRecords().size() > 0) {
            for (SysOrder record : mapperPage.getRecords()) {
                record.setStatusName(IBaseEnum.getLabelByValue(record.getStatus(), SysOrderStatusEnum.class));
            }
        }
        return mapperPage;
    }

    @Override
    public boolean saveSysOrder(SysOrderForm sysOrderForm) {
        SysOrder sysOrder = new SysOrder();
        BeanUtils.copyProperties(sysOrderForm, sysOrder);
        sysOrder.setCreateTime(null);
        return this.saveOrUpdate(sysOrder);
    }

    @Override
    public SysOrderForm getSysOrderFormData(Long id) {
        SysOrder sysOrder = this.getById(id);
        SysOrderForm sysOrderForm = new SysOrderForm();
        BeanUtils.copyProperties(sysOrder, sysOrderForm);
        return sysOrderForm;
    }

    @Override
    public boolean deleteSysOrder(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysOrderImportVo> voList) {
        List<SysOrder> sysOrderList = new ArrayList<>();
        List<String> shortNameList = voList.stream().map(SysOrderImportVo::getShortName).distinct().toList();
        List<SysAddress> sysAddressList = addressService.list(new LambdaQueryWrapper<SysAddress>().in(SysAddress::getShortName, shortNameList));
        SysOrder sysOrder;
        SysAddress address;
        for (SysOrderImportVo importVo : voList) {
            sysOrder = new SysOrder();
            BeanUtils.copyProperties(importVo, sysOrder);
            sysOrder.setWish("是");
            address = sysAddressList.stream().filter(item -> Objects.equals(item.getShortName(), importVo.getShortName())).findFirst().orElse(null);
            if(Objects.nonNull(address)){
                BeanUtils.copyProperties(address, sysOrder, "id");
            }
            sysOrder.setQty(NumberUtils.convertInteger(importVo.getQty()));
            sysOrder.setCreateTime(LocalDateTime.now());
            sysOrder.setUpdateTime(null);
            sysOrderList.add(sysOrder);
        }
        this.saveOrUpdateBatch(sysOrderList);
    }

    @Override
    public List<SysOrderImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysOrder> sysOrderList = this.getBaseMapper().selectBatchIds(ids);
        List<SysOrderImportVo> voList = new ArrayList<>();
        for (SysOrder sysOrder : sysOrderList) {
            SysOrderImportVo vo = new SysOrderImportVo();
            BeanUtils.copyProperties(sysOrder, vo);
            voList.add(vo);
        }
        return voList;
    }
}
